package com.mdlib.helper.ui.fragments.rush.me;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.model.entity.MessageEntity;
import com.mdlib.helper.model.entity.WebViewEntity;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.ui.adapter.RushMessageAdapter;
import com.mdlib.helper.ui.widget.springview.DefaultFooter;
import com.mdlib.helper.ui.widget.springview.HelperHeads;
import com.mdlib.helper.ui.widget.springview.SpringView;
import com.mdlib.helper.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：HQY on 16/11/30 16:16
 * 邮箱：hqy_xz@126.com
 */

public class RushMessageFragment extends BaseTitleFragment {


    @Bind(R.id.rv_rush_message)
    RecyclerView mRvRushMessage;
    @Bind(R.id.sv_rush_message)
    SpringView mSvRushMessage;

    private List<MessageEntity> messageList = new ArrayList<>();

    private RushMessageAdapter mRushMessageAdapter;
    private int pageNumber = 1;

    public static RushMessageFragment newInstance() {
        return new RushMessageFragment();
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_rush_message;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.me_tv_message, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getActivity().finish();
                    }
                });
        init();
    }

    public void init() {

        mRushMessageAdapter = new RushMessageAdapter(messageList);
        mRvRushMessage.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvRushMessage.setAdapter(mRushMessageAdapter);
        mRvRushMessage.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }

            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemClick(adapter, view, position);
                UIHelper.showWeb(getActivity(), new WebViewEntity(messageList.get(position).getTitle(),
                        messageList.get(position).getUrl()));
            }
        });
        mSvRushMessage.setType(SpringView.Type.FOLLOW);
        mSvRushMessage.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                pageNumber=1;
                getRushMessage();
            }

            @Override
            public void onLoadmore() {
                getRushMessage();
            }
        });
        mSvRushMessage.setHeader(new HelperHeads(getActivity()));
        mSvRushMessage.setFooter(new DefaultFooter(getActivity()));
        getRushMessage();
    }

    private void getRushMessage() {
        addSubscribe(DataManager.getInstance().getUserApi()
                .getRushMessageInfo(pageNumber + "")
                .compose(RxHelper.<ArrayList<MessageEntity>>defaultHandleResult())
                .subscribe(new RxSubscribe<ArrayList<MessageEntity>>() {
                    @Override
                    protected void onSucc(ArrayList<MessageEntity> messageEntities) {
                        pageNumber++;
                        updateList(messageEntities);
                        mSvRushMessage.onFinishFreshAndLoad();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                        mSvRushMessage.onFinishFreshAndLoad();

                    }
                }));
    }

    public void updateList(List<MessageEntity> messageEntities) {

        if (messageEntities.size() > 0 && pageNumber >= 2) {
            for (MessageEntity messageEntity : messageEntities) {
                messageList.add(messageEntity);
            }
            mRushMessageAdapter.notifyDataSetChanged();
        } else if (messageEntities.size() <= 0 && pageNumber > 2) {
            pageNumber--;
            ToastUtil.showToast("没有更多数据了!");
        }
    }

}
